// pages/order/order.js
Page({
  // 页面的初始数据
  data: {
    currentTab:0,
    userName:"",
    miniOrderList:""
  },
  // 交换标题·
  swichTab: function (e) {
    console.log(e)
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    this.update()
  },
  // 更新
  update: function(){
    // 1 取出缓存中的用户名
    var userName = wx.getStorageSync("username")
    this.setData(
      { userName: userName }
    )
    if (this.data.currentTab == 0)
    {
      wx.request({
        data: { userName: this.data.userName },
        url: 'http://192.168.166.182:8080/findTodayMiniOrder',
        success: (result) => {
          var tmp = result.data
          var img = []
          console.log(tmp)
          for (var i = 0; i < tmp.length;i ++) {
            img = []
            for (var j = 0; j < tmp[i].drinkIdList.length; j++) {
              img[j] = '/images/cofes/' + tmp[i].imgs[j];
            }
            tmp[i].img = img;
          }
          for (var i = 0; i < tmp.length;i++) {
            var d = new Date(tmp[i].orderTime);
            var y = d.getFullYear();
            var mon = d.getMonth() + 1;
            var day = d.getDate();
            var h = d.getHours(); // 12
            var m = d.getMinutes(); // 12
            var s = d.getMinutes();
            if (h < 10) {
              h = "0" + h;
            }
            if (m < 10) {
              m = "0" + m;
            }
            tmp[i].orderTime = y + '/' + mon + '/' + day + ' ' +h + ':' + m +':'+s
          }
          this.setData(
            { miniOrderList: tmp }
          )
        }
      })
    } else {
      wx.request({
        data: { userName: this.data.userName },
        url: 'http://192.168.166.182:8080/findAllMiniOrder',
        success: (result) => {
          var tmp = result.data
          console.log(tmp)
          var img=[]
          for (var i=0;  i< tmp.length; i++) {
            img = []
            for (var j = 0; j < tmp[i].drinkIdList.length;j++)
            {
              img[j] = '/images/cofes/' + tmp[i].imgs[j];
            }  
            tmp[i].img=img;
          }
          for (var i = 0; i < tmp.length; i++) {
            var d = new Date(tmp[i].orderTime);
            var y = d.getFullYear();
            var mon = d.getMonth() + 1
            var day = d.getDate();
            var h = d.getHours();//12
            var m = d.getMinutes(); //12
            var s = d.getMinutes();
            if (h < 10) {
              h = "0" + h;
            }
            if (m < 10) {
              m = "0" + m;
            }
            tmp[i].orderTime = y + '/' + mon + '/' + day + ' ' + h + ':' + m + ':' + s
          }
          this.setData(
            { miniOrderList: tmp }
          )
        }
      })
    }
  },
  // 展示订单详情页
  showDetail: function (e) {
    console.log(e.currentTarget.dataset)
    var orderId = e.currentTarget.dataset.id
    //发送请求的api
    wx.request({
      data: { userName: this.data.userName, orderId: orderId },
      url: 'http://192.168.166.182:8080/findOneOrder',
      success: (result) => {
        console.log(result.data)
        wx.navigateTo({
          url: '../order/orderDetail/orderDetail?orderDetail=' + JSON.stringify(result.data),
        })
      }
    })
  },
  // 前往评价
  goEvaluate: function (e) {
    var item = e.currentTarget.dataset.item
    console.log(item.orderId)
    wx.navigateTo({
      url: './evaluate/evaluate?orderId=' + JSON.stringify(item.orderId) + '&userName=' + JSON.stringify(this.data.userName),
    })
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    this.update()
  },
  // 生命周期函数--监听页面初次渲染完成
  onReady: function () {

  },
  // 生命周期函数--监听页面显示
  onShow: function () {
    this.update()
  },
  // 生命周期函数--监听页面隐藏
  onHide: function () {

  },
  // 生命周期函数--监听页面卸载
  onUnload: function () {

  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {

  },
  // 用户点击右上角分享
  onShareAppMessage: function () {

  }
})